#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

int m, n, hitWall, p;

typedef struct TBumper{
	int x, y, val, cost;
} TBumper;
typedef struct TBall{
	int x, y, dir, life;
} TBall;

TBumper bumpers[3000];
TBall ball;

int incx[] = {1, 0, -1, 0};
int incy[] = {0, 1, 0, -1};
int maps[52][52];


bool needRebound(int x, int y) {
	if (x <= 1 || x >= m)
		return true;
	if (y <= 1 || y >= n)
		return true;
	if (maps[x][y] != -1)
		return true;
	return false;
}

int main(void)
{
	scanf(" %d %d", &m, &n);
	scanf(" %d", &hitWall);
	scanf(" %d", &p);
	for (int i=0; i<52; ++i)
		for (int j=0; j<52; ++j)
			maps[i][j] = -1;
	for (int i=0; i<p; ++i) {
		scanf(" %d %d %d %d", &bumpers[i].x, &bumpers[i].y, &bumpers[i].val, &bumpers[i].cost);
		maps[bumpers[i].x][bumpers[i].y] = i;
	}
	for (int i=1; i<=n; ++i) {
		maps[1][i] = p;
		maps[m][i] = p;
	}
	for (int i=1; i<=m; ++i) {
		maps[i][1] = p;
		maps[i][n] = p;
	}
	bumpers[p].x = bumpers[p].y = -1;
	bumpers[p].cost = hitWall;
	bumpers[p].val = 0;

	int points, total(0);
	while (scanf(" %d %d %d %d", &ball.x, &ball.y, &ball.dir, &ball.life) == 4) {
		points = 0;
		while (ball.life > 1) {
//			cout << ball.x << " " << ball.y << " " << ball.dir << " " << ball.life << endl;
			int nx, ny;
			nx = ball.x + incx[ball.dir];
			ny = ball.y + incy[ball.dir];
			--ball.life;
//			cout << nx << "," << ny << " " << maps[nx][ny] << endl;
			if (needRebound(nx, ny)) {
				--ball.dir;
				if (ball.dir < 0) ball.dir += 4;
				ball.life -= bumpers[maps[nx][ny]].cost;
				points += bumpers[maps[nx][ny]].val;
			}
			else {
				ball.x = nx;
				ball.y = ny;
			}
		}
		total += points;
		printf("%d\n", points);
	}
	printf("%d\n", total);
	return 0;
}